/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.view.ev;

import offset.nodes.client.veditor.model.ev.DataElementView;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.Element;
import offset.nodes.client.chooser.view.QueryResultChooserDialog;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.virtual.model.jcr.nodetype.ValueConstraintDefinition;
import offset.nodes.client.view.SimpleDialog;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.veditor.model.DataModel;

/**
 *
 * @author Walter Lütgenau
 */
public class ReferenceFromTypeChooser implements DataElementView {

    DataElementViewContext context = null;
    String nodeType;

    public ReferenceFromTypeChooser(ValueConstraintDefinition[] valueConstraints) {
        nodeType = valueConstraints[0].getDefinition();
    }

    public void setContext(DataElementViewContext context) {
        try {
            this.context = context;
            Map properties = context.getVirtualBlock().getModel().getProperties();
            String serverUrl = (String) properties.get(Editor.PROP_UPLOAD_TO);
            ServerModel server = new ServerModel(new URL(serverUrl));

            QueryResultChooserDialog dialog = new QueryResultChooserDialog(new javax.swing.JFrame(),
                    true, server, "//element(*," + nodeType + ")");
            dialog.setVisible(true);

            if (dialog.getReturnStatus() == SimpleDialog.OK) {
                String uuid = dialog.getUuid();
                String name = dialog.getName();
                if (uuid == null) {
                    JOptionPane.showMessageDialog(null, ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("selectNode.invalidNode"));

                    return;
                }
                 updateSchemaTree(context.getElement(), name, uuid);
            }
        } catch (Exception ex) {
            Logger.getLogger(ReferenceFromTypeChooser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    protected void updateSchemaTree(Element element, String name, String uuid) throws Exception {
        DataModel model = context.getVirtualBlock().getModel();

        StringBuffer buf = new StringBuffer();

        buf.append(model.getStartTag(element));
        buf.append("<a href=\"" + uuid + "\">" + name + "</a>");
        buf.append(model.getEndTag(element));
        
        model.setOuterHTML(element, buf.toString());
    }

 }
